package com.justech.eat.service.component;

import org.apache.commons.lang.StringUtils;

import javax.servlet.http.HttpServletRequest;

/**
 * @program: eat-service
 * @description: 已登录用户服务
 * @author: feifei.li
 * @create: 2020-06-15 15:47
 **/
public interface UserTokenService {

    String NATIVE = "native", WEB = "web";

    /**
     * 获取用户信息
     *
     * @param token 已加密token
     * @return
     */
    String getJobNumber(String token);

    /**
     * 通过token判断客户端类型
     *
     * @param token
     * @return native/web
     */
    default String getClientType(String token) {
        if (StringUtils.isBlank(token)) {
            throw new RuntimeException("token must not be empty");
        }
        return token.length() > 24 ? NATIVE : WEB;
    }

    default String getToken(HttpServletRequest request) {
        String token = request.getParameter("token");
        if (StringUtils.isBlank(token)) {
            token = request.getHeader("token");
            if (StringUtils.isBlank(token)) {
                token = request.getHeader("Authorization");
            }
        }
        return token;
    }

    default String getJobNumber(HttpServletRequest request) {
        return getJobNumber(getToken(request));
    }
}
